﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Authentication;
using System.Web.Security;
using MPic.Business.Entity;
using MPic.Business.BLL;

namespace MPic.Common
{
    /// <summary>
    /// 当前登录用户信息
    /// </summary>
    public class CurrentUser
    {
        public CurrentUser()
        {
            if (!HttpContext.Current.User.Identity.IsAuthenticated || (HttpContext.Current.User.Identity as FormsIdentity) == null)
            {
                throw new AuthenticationException("账户未登录");//必须要用户Form验证后才能使用CurrentUser
            }
            FormsAuthenticationTicket ticket = (HttpContext.Current.User.Identity as FormsIdentity).Ticket;
            string[] userInfo = ticket.UserData.Split(';');
            UserName = ticket.Name;
            UserId = int.Parse(userInfo[0]);
            //从缓存中取得当前登录用户信息，缓存无，则从数据库取得
            Users = UsersBLL.Instance.GetUser(UserName);// UserRedisBLL.Instance.SignInByUserCodeRedis(UserName);
            //if (Users == null)
            //{
            //Users = UserBLL.Instance.GetUserByName(UserName);
            //UserRedisBLL.Instance.SignInSessionRedis(Users);
            //}
        }

        public UsersEnitty Users { get; set; }

        public int UserId { get; set; }

        public string UserName { get; set; }
    }
}